//
//  CYBindCardSMSCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/19.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYBindCardSMSCell.h"

#import "CYGlobalTimerManager.h"
#import "CYSMSExpireManager.h"



@implementation CYBindCardSMSCellObject

@end



@interface CYBindCardSMSCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UITextField *smsTextField;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UIButton *smsButton;

@property(nonatomic, strong)CYBindCardSMSCellObject *object;

@end


@implementation CYBindCardSMSCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.text = @"验证码";
        [self.titleLabel sizeToFit];
        [self.contentView addSubview:self.titleLabel];
        
        self.smsTextField = [[TCNoActionTextField alloc] init];
        self.smsTextField.returnKeyType = UIReturnKeyDone;
        self.smsTextField.keyboardType = UIKeyboardTypeNumberPad;
        self.smsTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
        self.smsTextField.textColor = HEXCOLOR(@"444444");
        self.smsTextField.font = [UIFont systemFontOfSize:16];
        [self.smsTextField addTarget:self.smsTextField action:@selector(resignFirstResponder) forControlEvents:UIControlEventEditingDidEndOnExit];
        [self.smsTextField addTarget:self action:@selector(onSMSTextFieldEditingChanged) forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:self.smsTextField];
        
        self.line = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.line];
        
        self.smsButton = [[UIButton alloc] init];
        self.smsButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [self.smsButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.smsButton setTitleColor:APP_DEFAULT_COLOR_BLUE forState:UIControlStateNormal];
        [self.smsButton setTitleColor:HEXCOLORA(@"3E8CE6", 0.6) forState:UIControlStateHighlighted];
        [self.smsButton setTitleColor:HEXCOLOR(@"CCCCCC") forState:UIControlStateDisabled];
        self.smsButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        [self.smsButton addTarget:self action:@selector(onSMSButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.smsButton];
        @weakify(self);
        [CYStandardGlobalTimerManager addTarget:self withBlock:^{
            @strongify(self);
            [self checkSMSButton];
        }];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 50;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    self.smsTextField.text = self.object.smsCode;
    
    [self checkSMSButton];
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.centerY = self.contentView.height / 2;
    self.titleLabel.left = 20;
    
    self.line.width = 1 / SCREEN_SCALE;
    self.line.height = 30;
    self.line.centerY = self.contentView.height / 2;
    self.line.right = self.contentView.width - 120;
    
    self.smsTextField.width = self.line.centerX - 20 - 114;
    self.smsTextField.height = self.contentView.height;
    self.smsTextField.left = 114;
    self.smsTextField.top = 0;
    
    self.smsButton.width = 100;
    self.smsButton.height = self.contentView.height;
    self.smsButton.left = self.line.centerX;
    self.smsButton.top = 0;
}

#pragma mark - Control Events
- (void)onSMSTextFieldEditingChanged
{
    self.object.smsCode = self.smsTextField.text;
}

- (void)onSMSButtonTouchUpInside
{
    if(self.object.smsCodeAction)
    {
        self.object.smsCodeAction();
    }
}

#pragma mark - Private Method
- (void)checkSMSButton
{
    NSTimeInterval seconds = [CYStandardSMSExpireManager secondsWithType:self.object.type];
    if(seconds <= 0)
    {
        self.smsButton.enabled = YES;
    }
    else
    {
        self.smsButton.enabled = NO;
        [self.smsButton setTitle:[NSString stringWithFormat:@"%.0fs后获取", seconds] forState:UIControlStateDisabled];
    }
}

@end
